<?php
namespace App\Services\Myself;

use App\Services\CommonService;
use Illuminate\Support\Facades\DB;
use App\Model\Tag;
use App\Model\Collect;
class CollectService extends CommonService{
    /**
     * 分页
     */
    public function page($page,$type,$uid,$len){
        $rowsum=Collect::where('store_uid',$uid)->where('store_type',$type)->distinct('store_id')->count('store_id');
        $pagesum=ceil($rowsum/$len);
        if($page<=0){
            $page=1;
        }
        if($page>$pagesum){
            $page=$pagesum;
        }
        $limit=($page-1)*$len;
        return $limit;
    }
    /**
     * 我的收藏   企业
     */
    public function CollectEnterprise($uid,$page){
        $len=5;
        $limit=$this->page($page,5,$uid,$len);
        //收藏表和企业表联查
        $arr=Collect::join('re_enterprise as e','e.id','=','re_store.store_rela_id')
            ->where('store_uid',$uid)
            ->where('store_type',5)
            ->Take($len)
            ->Skip($limit)
            ->get(['e.id','e.enterprise_name','e.enterprise_logo','e.enterprise_tap_id']);
        //查出标签
        $res = Tag::where('reg_type',2)->get(['reg_id','reg_name'])->toArray();
//        return $res;
        foreach($arr as $k=>$v){
            foreach($res as $kk=>$vv){
                if(in_array($vv['reg_id'],json_decode($v->enterprise_tap_id))){
                    $arr[$k]->reg_name.=$vv['reg_name'].',';
                }
            }
            $arr[$k]->reg_name=substr($v->reg_name,0,-1);
        }
        return $this->success($arr);
    }
    /**
     * 我的收藏   职位
     */
    public function CollectPosition($uid,$page){
        $len=5;
        $limit=$this->page($page,5,$uid,$len);
        //收藏表和职位表联查
        $arr=Collect::join('re_position as p','p.position_id','=','re_store.store_rela_id')
            ->join('re_enterprise as e','e.id','=','p.company_id')
            ->where('store_uid',$uid)
            ->where('store_type',3)
            ->Take($len)
            ->Skip($limit)
            ->groupBy('p.position_id')
            ->get(['p.position_id','p.position_name','p.position_address','p.position_education','e.enterprise_name','e.enterprise_image','p.position_moneymin','p.position_moneymax']);
        foreach($arr as $v){
            switch ($v->position_education){
                //代表初中  2代表高中  3代表大专  4代表本科
                case 1:$v->position_education='初中';break;
                case 2:$v->position_education='高中';break;
                case 3:$v->position_education='大专';break;
                case 4:$v->position_education='本科';break;
                default :$v->position_education='';break;
            }
        }
        return $this->success($arr);
    }
    /**
     * 我的收藏   咨询
     */
    public function CollectInfomation($uid,$page){
        $len=5;
        $limit=$this->page($page,5,$uid,$len);
        //收藏表和咨询表联查
        $arr=Collect::join('re_information as i','i.id','=','re_store.store_rela_id')
            ->where('store_uid',$uid)
            ->where('store_type',1)
            ->Take($len)
            ->Skip($limit)
            ->groupBy('i.id')
            ->get(['i.id','i.name','i.img','i.content','i.time'])
            ->toArray();
        foreach($arr as &$v){
            $v['desc']=mb_substr($v['content'],0,50);
        }
        return $this->success($arr);
    }
}
//foreach ($arr as $k => $v){
//    if (count($tap_id=json_decode($v->enterprise_tap_id))>0){
//        foreach ($tap_id as $kk => $vv){
//            foreach($res as $k1=>$v1){
//                if ($vv==$v1['reg_id']){
//                    $arr[$k]['reg_name'].=','.$v1['reg_name'];
//                }
//            }
//        }
//        $arr[$k]['reg_name'] = substr($arr[$k]['reg_name'],'1');
//    }
//}